package cn.xz.xy.net;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Handler;

public class NetWorkUtil
{
    // Private fields
    private static final String TAG = NetWorkUtil.class.getSimpleName();
    private static final int EXPECTED_SIZE_IN_BYTES = 1048576;// 1MB 1024*1024
    private static final double BYTE_TO_KILOBIT = 0.0078125;
    private static final double KILOBIT_TO_MEGABIT = 0.0009765625;
 
    private static Handler mHandler;
    public static int timer;
 
    // 网络状态，连接wifi，cmnet是直连互联网的，cmwap是需要代理，noneNet是无连接的
    // 一速度来说：wifi > cmnet >cmwap > noneNet
    public static enum netType
    {
        wifi, CMNET, CMWAP, noneNet
    }
 
    /**
     * 网络是否可用
     * 
     * @param context
     * @return
     */
    public static boolean isNetworkAvailable(Context context)
    {
        // 获取网络manager
        ConnectivityManager mgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo[] info = mgr.getAllNetworkInfo();
 
        // 遍历所有可以连接的网络
        if (info != null)
        {
            for (int i = 0; i < info.length; i++)
            {
                if (info[i].getState() == NetworkInfo.State.CONNECTED)
                {
                    return true;
                }
            }
        }
        return false;
    }
 
    /**
     * 判断是否有网络连接
     * 
     * @param context
     * @return
     */
    public static boolean isNetworkConnected(Context context)
    {
        if (context != null)
        {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
            if (mNetworkInfo != null)
            {
                return mNetworkInfo.isAvailable();
            }
        }
        return false;
    }
 
    /**
     * 判断WIFI网络是否可用
     * 
     * @param context
     * @return
     */
    public static boolean isWifiConnected(Context context)
    {
        if (context != null)
        {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mWiFiNetworkInfo = mConnectivityManager
                .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            if (mWiFiNetworkInfo != null)
            {
                return mWiFiNetworkInfo.isAvailable();
            }
        }
        return false;
    }
 
    /**
     * 判断MOBILE网络是否可用
     * 
     * @param context
     * @return
     */
    public static boolean isMobileConnected(Context context)
    {
        if (context != null)
        {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mMobileNetworkInfo = mConnectivityManager
                .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            if (mMobileNetworkInfo != null)
            {
                return mMobileNetworkInfo.isAvailable();
            }
        }
        return false;
    }
 
    /**
     * 获取当前网络连接的类型信息
     * 
     * @param context
     * @return
     */
    public static int getConnectedType(Context context)
    {
        if (context != null)
        {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
            if (mNetworkInfo != null && mNetworkInfo.isAvailable())
            {
                return mNetworkInfo.getType();
            }
        }
        return -1;
    }
 
   /* *//**
     * 
     * @author 
     * 
     *         获取当前的网络状态 -1：没有网络 1：WIFI网络2：wap 网络3：net网络
     * 
     * @param context
     * 
     * @return
     *//*
    public static netType getAPNType(Context context)
    {
        ConnectivityManager connMgr = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
        if (networkInfo == null)
        {
            return netType.noneNet;
        }
        int nType = networkInfo.getType();
 
        if (nType == ConnectivityManager.TYPE_MOBILE)
        {
            if (networkInfo.getExtraInfo().toLowerCase().equals(cmnet))
            {
                return netType.CMNET;
            }
 
            else
            {
                return netType.CMWAP;
            }
        } else if (nType == ConnectivityManager.TYPE_WIFI)
        {
            return netType.wifi;
        }
        return netType.noneNet;
 
    }
 
    *//**
     * 测试网速
     * 
     * @param handler
     *//*
    public static void textSpeed(Handler handler)
    {
        mHandler = handler;
        new Thread(mWorker).start();
    }
 
    *//**
     * Our Slave worker that does actually all the work
     *//*
    private static final Runnable mWorker = new Runnable()
    {
 
        @Override
        public void run()
        {
            InputStream stream = null;
            try
            {
                int bytesIn = 0;
                String downloadFileUrl = http://120.24.237.77/test;
                long startCon = System.currentTimeMillis();
                URL url = new URL(downloadFileUrl);
                URLConnection con = url.openConnection();
                con.setUseCaches(false);
                long connectionLatency = System.currentTimeMillis() - startCon;
                stream = con.getInputStream();
 
                Message msgUpdateConnection = Message.obtain(mHandler,
                    APPConstant.MSG_UPDATE_CONNECTION_TIME);
                msgUpdateConnection.arg1 = (int) connectionLatency;
                mHandler.sendMessage(msgUpdateConnection);
 
                long start = System.currentTimeMillis();
                int currentByte = 0;
                long updateStart = System.currentTimeMillis();
                long updateDelta = 0;
                int bytesInThreshold = 0;
 
                while ((currentByte = stream.read()) != -1)
                {
                    bytesIn++;
                    bytesInThreshold++;
                    if (updateDelta >= APPConstant.UPDATE_THRESHOLD)
                    {
                        int progress = (int) ((bytesIn / (double) EXPECTED_SIZE_IN_BYTES) * 100);
                        Message msg = Message.obtain(mHandler, APPConstant.MSG_UPDATE_STATUS,
                            calculate(updateDelta, bytesInThreshold));
                        msg.arg1 = progress;
                        msg.arg2 = bytesIn;
                        mHandler.sendMessage(msg);
                        // Reset
                        updateStart = System.currentTimeMillis();
                        bytesInThreshold = 0;
                    }
                    updateDelta = System.currentTimeMillis() - updateStart;
                }
 
                long downloadTime = (System.currentTimeMillis() - start);
                // Prevent AritchmeticException
                if (downloadTime == 0)
                {
                    downloadTime = 1;
                }
 
                Message msg = Message.obtain(mHandler, APPConstant.MSG_COMPLETE_STATUS,
                    calculate(downloadTime, bytesIn));
                msg.arg1 = bytesIn;
                mHandler.sendMessage(msg);
            } catch (MalformedURLException e)
            {
                Log.e(TAG, e.getMessage());
            } catch (IOException e)
            {
                Log.e(TAG, e.getMessage());
            } finally
            {
                try
                {
                    if (stream != null)
                    {
                        stream.close();
                    }
                } catch (IOException e)
                {
                    // Suppressed
                }
            }
 
        }
    };
 
    *//**
     * 
     * 1 byte = 0.0078125 kilobits 1 kilobits = 0.0009765625 megabit
     * 
     * @param downloadTime
     *          in miliseconds
     * @param bytesIn
     *          number of bytes downloaded
     * @return SpeedInfo containing current speed
     *//*
    private static SpeedInfo calculate(final long downloadTime, final long bytesIn)
    {
        SpeedInfo info = new SpeedInfo();
        // from mil to sec
        long bytespersecond = (bytesIn / downloadTime) * 1000;
        double kilobits = bytespersecond * BYTE_TO_KILOBIT;
        double megabits = kilobits * KILOBIT_TO_MEGABIT;
        info.downspeed = bytespersecond;
        info.kilobits = kilobits;
        info.megabits = megabits;
 
        return info;
    }
 */
    /**
     * Transfer Object
     * 
     * @author devil
     *
     */
    public static class SpeedInfo
    {
        public double kilobits = 0;
        public double megabits = 0;
        public double downspeed = 0;
    }
}